home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / OCI72 / CALLDEMO.SQL next >
Encoding:
Text File  |  1995-05-18  |  2.7 KB  |  84 lines

  1. rem 
  2. rem $Header: calldemo.sql 7020100.1 94/09/23 22:19:28 cli Generic<base> $ 
  3. rem 
  4. Rem  Copyright (c) 1991 by Oracle Corporation 
  5. Rem    NAME
  6. Rem      calldemo.sql - <one-line expansion of the name>
  7. Rem    DESCRIPTION
  8. Rem      <short description of component this file declares/defines>
  9. Rem    RETURNS
  10. Rem 
  11. Rem    NOTES
  12. Rem      <other useful comments, qualifications, etc.>
  13. Rem    MODIFIED   (MM/DD/YY)
  14. Rem     rkooi2     11/27/92 -  Creation 
  15. CREATE OR REPLACE PACKAGE calldemo AS
  16.  
  17.    TYPE char_array IS TABLE OF VARCHAR2(20)
  18.        INDEX BY BINARY_INTEGER;
  19.    TYPE num_array IS TABLE OF FLOAT
  20.        INDEX BY BINARY_INTEGER;
  21.  
  22.    PROCEDURE get_employees(
  23.      dept_number IN     number,    -- department to query
  24.      batch_size  IN     INTEGER,   -- rows at a time
  25.      found       IN OUT INTEGER,   -- rows actually returned
  26.      done_fetch  OUT    INTEGER,   -- all done flag
  27.      emp_name    OUT    char_array,
  28.      job         OUT    char_array,
  29.      sal         OUT    num_array);
  30.  
  31. END calldemo;
  32. /
  33.  
  34. CREATE OR REPLACE PACKAGE BODY calldemo AS
  35.  
  36.    CURSOR get_emp (dept_number IN number) IS
  37.        SELECT ename, job, sal FROM emp
  38.            WHERE deptno = dept_number;
  39.  
  40.    -- Procedure "get_employees" fetches a batch of employee
  41.    -- rows (batch size is determined by the client/caller
  42.    -- of the procedure).  It can be called from other
  43.    -- stored procedures or client application programs.
  44.    -- The procedure opens the cursor if it is not
  45.    -- already open, fetches a batch of rows, and
  46.    -- returns the number of rows actually retrieved. At
  47.    -- end of fetch, the procedure closes the cursor.
  48.  
  49.    PROCEDURE get_employees(
  50.      dept_number IN     number,
  51.      batch_size  IN     INTEGER,
  52.      found       IN OUT INTEGER,
  53.      done_fetch  OUT    INTEGER,
  54.      emp_name    OUT    char_array,
  55.      job         OUT    char_array,
  56.      sal         OUT    num_array) IS
  57.  
  58.    BEGIN
  59.        IF NOT get_emp%ISOPEN THEN      -- open the cursor if
  60.            OPEN get_emp(dept_number);  -- not already open
  61.        END IF;
  62.  
  63.        -- Fetch up to "batch_size" rows into PL/SQL table,
  64.        -- tallying rows found as they are retrieved. When all
  65.        -- rows have been fetched, close the cursor and exit
  66.        -- the loop, returning only the last set of rows found.
  67.  
  68.        done_fetch := 0;  -- set the done flag FALSE
  69.        found := 0;
  70.  
  71.        FOR i IN 1..batch_size LOOP
  72.            FETCH get_emp INTO emp_name(i), job(i), sal(i);
  73.            IF get_emp%NOTFOUND THEN    -- if no row was found
  74.                CLOSE get_emp;
  75.                done_fetch := 1;   -- indicate all done
  76.                EXIT;
  77.            ELSE
  78.                found := found + 1;  -- count row
  79.            END IF;
  80.        END LOOP;
  81.    END;
  82. END;
  83. /
  84.